<?php 

    public function actionImport()
    {
      if (Yii::$app->request->isPost && Yii::$app->request->post('import')){
        $result = array();
        $errors = array();
        $head = ['country' =>'国家',
                  'game' =>'版本', 
                  'platform' => '平台', 
                  'order_coins'=> '金币数', 
                  'customer_name'=> '评论人', 
                  'stars'=> '星星数', 
                  'showtime'=>'时间',
                  'comment' => '内容'
                ];
        $filename = $_FILES['data_csv']['tmp_name'];
      
        if (!empty ($filename))
        {
            $handle = fopen($filename, 'r');
            $lines = array();
            $datahead = array();
            $n = 0;
            $flip = array_flip($head);
            
            while ($fields = fgetcsv($handle, 10000)) {
              
                $num = count($fields);
                if (empty($datahead))
                {
                    $datahead = $fields;
                    continue;
                }
                for ($i = 0; $i < $num; $i++) {                  
                    $keystr = $datahead[$i];
                    $lines[$n][$flip[$keystr]] = $fields[$i];
                }
                $n++;
            }
            fclose($handle); //关闭指针
            if (count($lines) > 0)
            {             
                //开始导入数据
                foreach ($lines as $li=>$line)
                { 
                    $return = $this->_importLine($line);
                    if ($return !== true)
                    {
                        $errors[] = 'Line '.($li + 1).': '.$return;
                    }
                }
            }
            else
            {
                $errors[] = '无数据';
            }
        }
        else
        {
            $errors[] = '请上传文件';
        }

        if (!empty($errors))
        {
            $status  = "error";
            $result = implode('&lt;br/&gt;', $errors);
        }
        else
        {
            $status = 'success';
            $result = '导入成功';
        }
 
        return $this->msg($result, Url::to(['/comments/index']),$status);
         
    }
    return $this->renderPartial('import', Yii::$app->view->params);
    }


    protected function _importLine($line)
    {
      
        $form = new CommentsForm;
        $orm  =  new Comments;
        //处理数字被number_format格式化过
        $line['order_coins'] = intval(str_replace(",","",$line['order_coins']));

        $form->load(['CommentsForm' => $line]  );
        $msg = "";
          
        if (!$form->validate()) {
            foreach($form->errors as $key => $er){
                $msg .= sprintf("%s:%s", $key, $er[0]);
            }
            return $msg;
        }

        $post = $form->attributes;              
        $post['showtime'] = strtotime($post['showtime']) ? strtotime($post['showtime']) :time();
        $post['customer_id'] = Yii::$app->session['_managerID'];
        $post['stars'] = min(5, $post['stars']);
        $post['order_id'] = 0;
        $post['status'] = 1; //发布即审核通过

        $orm->attributes = $post;
        $orm->save();
          
        return true;
    }